/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MessageSourceValidatorStatusPanel.java
 *
 * Created on Dec 20, 2008, 11:45:35 AM
 */

package org.robcash.apps.messagepopup.view.components;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;
import org.robcash.apps.messagepopup.Main;
import org.robcash.commons.event.TaskEvent;
import org.robcash.commons.event.TaskListener;
import org.robcash.commons.swing.wizard.WizardComponent;

/**
 *
 * @author Rob
 */
public class MessageSourceValidatorStatusPanel extends WizardComponent
        implements TaskListener {

    private Future<Boolean> validationTask;

    private ResourceMap resources;

    /** Creates new form MessageSourceValidatorStatusPanel */
    public MessageSourceValidatorStatusPanel() {
        resources = Application.getInstance(Main.class).getContext()
                .getResourceMap(MessageSourceValidatorStatusPanel.class);
        initComponents();
    }

    /**
     * Set the validation task to monitor
     * @param task Background task
     */
    public void setValidationTask(Future<Boolean> task) {
        this.validationTask = task;
    }

    /**
     * Receive notification that a task has started
     * @param event Task event
     */
    public void taskStarted(TaskEvent event) {
        progressBar.setIndeterminate(true);
    }

    /**
     * Receive notification that of a task's status
     * @param event Task event
     */
    public void taskInProgress(TaskEvent event) {
        if ( event.getPercentComplete() < 0 ) {
            progressBar.setIndeterminate(true);
        }
        else {
            progressBar.setIndeterminate(false);
            progressBar.setValue(event.getPercentComplete());
        }
    }

    /**
     * Receive notification that a task has completed
     * @param event Task event
     */
    public void taskCompleted(TaskEvent event) {
        // Mark progress as complete
        progressBar.setIndeterminate(false);
        progressBar.setValue(100);

        // Check on the outcome
        Boolean validationResult = Boolean.FALSE;
        try {
            validationResult = validationTask.get(500, TimeUnit.MILLISECONDS);
            if ( validationResult.booleanValue() ) {
                status.setText(resources.getString("validation.success"));
            }
            else {
                status.setText(resources.getString("validation.failure"));
            }
        }
        catch(ExecutionException e) {
            // Validation failed
            status.setText(resources.getString("validation.failure"));
        }
        catch(Exception e) {
            // We failed to get validation results.
            // No matter the reason, we don't want to return a next panel
            status.setText(resources.getString("validation.error"));
        }

        // Notify listeners
        fireChangeListenerStateChanged();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        instructions = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        status = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.robcash.apps.messagepopup.Main.class).getContext().getResourceMap(MessageSourceValidatorStatusPanel.class);
        instructions.setText(resourceMap.getString("instructions.text")); // NOI18N
        instructions.setName("instructions"); // NOI18N

        progressBar.setIndeterminate(true);
        progressBar.setName("progressBar"); // NOI18N

        status.setText(resourceMap.getString("validation.inProgress")); // NOI18N
        status.setName("status"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(status, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                    .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                    .addComponent(instructions, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(instructions)
                .addGap(18, 18, 18)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(status)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel instructions;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel status;
    // End of variables declaration//GEN-END:variables

}
